﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCM.UI.Utils;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Telerik.Windows.Controls;

/// <summary>
/// Created by 李红星 2011-6-21
/// 材料费用额度管理
/// </summary>
namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCM41 : EFViewCmes
    {
        private MonthBlock monthblock = new MonthBlock();//构造前台下拉列表
        public ZMCM41()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(monthblock.GetMonth(true));
            base.Initialize(initData);

        }
        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 材料费用额度管理新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            //生成
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCM41");
            eiInfo.Set("method", "getBeforeMonth");
            eiInfo.Set("fdDepartmentId", this.deptId.Value.ToString());
            //eiInfo.Set("fdNo", this.number.Value.ToString());
            //eiInfo.Set("tableName", this.fdParentTable.Value.ToString());

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, CallServiceCompleted);

            //EIInfo eiInfo = new EIInfo();
//            base.OnInsertButtonClickPopup(eiInfo, "材料费用额度管理-新增", "insert", null, sender, e);
        }
        /// <summary>
        /// 访问后台的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CallServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo == null)
            {
                base.OpenEFWindowAlert("操作失败！", true);
            }
            else
            {
                this.OpenPopup(e.EIInfo, "材料费用额度管理-新增", "insert", null);
                //EIInfo eiInfo= e.EIInfo;
                //base.OnInsertButtonClickPopup(eiInfo, "材料费用额度管理-新增", "insert", null, sender, e);
            }
        }


        /// <summary>
        /// 材料费用额度管理修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "材料费用额度管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }

        /// <summary>
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            MaterialRation materialRation = new MaterialRation(dialogType);//构造一个新的修改页面
            materialRation.EIDataContext = new Platform.Core.UI.EIDataContext();
            eIInfo.AddBlock(monthblock.GetMonth());  //月份下拉框
            if (dialogType.Equals("insert"))
            {
                materialRation.fdYear.Value = DateTime.Now.Year; //新增时年份默认当前年
                materialRation.fdMonth.Value = DateTime.Now.Month.ToString();

            }
            materialRation.fdDepartmentId.Value = this.deptId.Value;//给新增界面单位id置 树节点id
            materialRation.deptName.Value = this.deptName.Value;//给新增界面单位名称置 树节点名称
            
            materialRation.EIDataContext.Update(eIInfo);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = materialRation;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();
        }
    }
}
